package com.example.agentai.controller;


import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {

    private final ChatClient chatClient;

    @RequestMapping(value = "/chat", produces = "text/html;charset=UTF-8")
    public Flux<String> chat(@RequestParam(defaultValue = "讲个笑话") String prompt, @RequestParam("chatId") String chatId) {
        return chatClient
                .prompt() // 传入user提示词
                .user(prompt)
                .advisors(as -> as.param(ChatMemory.CONVERSATION_ID, chatId)) // 记录会话历史
                .stream() // 同步请求,流式回传
                .content(); // 返回相应内容
    }
}
